//
//  LGEvaluationReportController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/3/1.
//  报告中心 -- 评估报告

#import "LGEvaluationReportController.h"
#import "LGGetWebViewController.h"
#import "LGEvaluationReportCell.h"
#import "LGEvaluationReportModel.h"
#import "LGReportCenterHeaderView.h"
#import "SVGKit.h"
#import "SVGKImage.h"
#import "SVGKParser.h"
@interface LGEvaluationReportController ()<UITableViewDataSource,UITableViewDelegate>
/**   */
@property (nonatomic , strong)UIView *topLine;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSArray *dataArray;

@end

@implementation LGEvaluationReportController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [LGEvaluationReportModel mj_setupObjectClassInArray:^NSDictionary *{
        return @{@"lists":@"LGEvaluationModel"};
    }];
//    [self.view addSubview:self.topLine];
    [self.view addSubview:self.tableView];
    [self emptyViewWithSuperView:self.view frame:self.tableView.frame];
    self.emptyView.backgroundColor = [UIColor clearColor];
}

-(void)viewWillAppearAction{
    [self requestData];
}

-(void)requestData{
    [self.emptyView startBufferAction];
    [RequestUtil withPOST:@"/doctor/patient.evaluation/evasList" parameters:@{@"patient_id":self.patientid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(code == 200){
            self.dataArray = returnData;
            [self.tableView reloadData];
            if(self.dataArray.count == 0){
                [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无评估" offset:0];
            }
//            NSMutableArray *tempArray = [LGEvaluationReportModel mj_objectArrayWithKeyValuesArray:returnData];
//            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//                for (NSInteger i=0; i<tempArray.count; i++) {
//                    LGEvaluationReportModel *Model = tempArray[i];
//                    NSMutableArray *evaArray = Model.lists;
//                    for (NSInteger j=0; j<evaArray.count; j++) {
//                        LGEvaluationModel *model = evaArray[j];
//                        model.image = [SVGKImage imageWithContentsOfURL:[NSURL URLWithString:model.icon]].UIImage;
//                        [evaArray replaceObjectAtIndex:j withObject:model];
//                    }
//                    Model.lists = evaArray;
//                    [tempArray replaceObjectAtIndex:i withObject:Model];
//                }
//                self.dataArray = tempArray;
//                dispatch_async(dispatch_get_main_queue(), ^{
//                    [self.emptyView stopBufferAction];
//                    [self.tableView reloadData];
//                    if(self.dataArray.count == 0){
//                        [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无评估" offset:0];
//                    }
//                });
//            });
        }else{
            [self.emptyView stopBufferAction];
            if(self.dataArray.count == 0){
                [self.emptyView showViewWithImage:@"emptyView2" content:message offset:0];
            }
        }
        [self.tableView reloadData];
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:LGErrorMessage offset:0];
        }
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *tempArray = self.dataArray[section][@"list"];
    return tempArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGEvaluationReportCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataDic = self.dataArray[indexPath.section][@"list"][indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *tempDic = self.dataArray[indexPath.section][@"list"][indexPath.row];
    LGGetWebViewController *controller = [[LGGetWebViewController alloc]init];
//    controller.withAsset = YES;
    controller.urlStr = LGNSString(tempDic[@"report_url"]);
    [self.navigationController pushViewController:controller animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(30);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LGReportCenterHeaderView *headView = [[LGReportCenterHeaderView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(30))];
    headView.content = LGNSString(self.dataArray[section][@"date"]);
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return viewPix(5);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(5))];
    baseView.backgroundColor = [UIColor clearColor];
    return baseView;
}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.tableView.frame = CGRectMake(0, 0, Screen_W, self.view.bounds.size.height);
    self.emptyView.frame = self.tableView.frame;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGEvaluationReportCell class] forCellReuseIdentifier:@"cell"];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.rowHeight = viewPix(90);
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

-(UIView *)topLine{
    if(!_topLine){
        _topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, 1)];
        _topLine.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
    }
    return _topLine;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
